<script setup>
import { useUser } from "@/store/useUser";
import defaultAvatar from "@/assets/app-mj.png";
import { computed, ref } from "vue";
import { useRouter } from "vue-router";

const router = useRouter();
const props = defineProps({
  msg: {
    type: Object,
    default: {},
  },
});
const userStore = useUser();

// 头像
const headSrc = computed(() => {
  if (isMine.value && userStore.getUserInfo.avatar) {
    return userStore.getUserInfo.avatar;
  }
  if (!isMine.value && props.msg.fromAvatar) {
    return props.msg.fromAvatar;
  }
  return defaultAvatar;
});
// 是不是自己的消息
const isMine = computed(() => {
  return props.msg.from === userStore.getUserInfo.uimKey;
});
// 交付周期
const deliverDays = computed(() => {
  return props.msg.exData.orderDetails?.versionInfo?.deliverDays || "-";
});

// 跳转订单详情
const goToOrderDetails = () => {
  router.push(
    `/orderDetails?id=${props.msg.exData.orderId}&gid=${props.msg.exData.goodsId}`
  );
};
</script>

<template>
  <div class="text-msg">
    <el-image v-if="!isMine" class="head" :src="headSrc" alt="">
      <template #error>
        <div class="image-error">
          <img class="head" :src="defaultAvatar" alt="" />
        </div>
      </template>
    </el-image>
    <div class="msg-text" :class="{ mine: isMine }">
      <p class="name" v-if="!isMine">{{ props.msg.fromName }}</p>
      <div class="order" :class="{ mine: isMine }">
        <div class="order-item">
          <div class="header">
            <img src="@/assets/order-pay-icon.png" alt="" />
            <span>邀请支付-加价订单</span>
          </div>
          <div class="order-info">
            <img
              class="img"
              :src="props.msg.exData.goodsDetails.coverPic"
              alt=""
            />
            <div class="info">
              <p class="name">
                {{ props.msg.exData.goodsDetails.goodsName }}
              </p>
              <p class="price">
                <span class="days">{{ deliverDays }}天交付</span>
                <span class="size"
                  >¥{{ props.msg.exData.orderDetails.amount }}</span
                >
              </p>
            </div>
          </div>
          <div class="add-line">
            <p>
              <span>追加交付内容</span
              ><span>{{ props.msg.exData.reason || "-" }}</span>
            </p>
            <!-- <p><span>追加交付天数</span><span>+ {{props.msg.exData.reason || '-'}}天</span></p> -->
            <p>
              <span>追加服务费用</span
              ><span>+ ¥{{ props.msg.exData.amount || "-" }}</span>
            </p>
          </div>
          <p class="order-count">
            <span class="title">订单总计</span>
            <!-- <span class="day">5天交付</span> -->
            <span class="price"
              ><span>¥</span>{{ props.msg.exData.amount }}</span
            >
          </p>
          <router-link to="#">
            <p class="order-btn">
              <span class="confirm" @click="goToOrderDetails">查看</span>
            </p>
          </router-link>
        </div>
      </div>
      <i v-if="isMine">{{ props.msg.read ? "已读" : "未读" }}</i>
    </div>
    <el-image v-if="isMine" class="head" :src="headSrc" alt="">
      <template #error>
        <div class="image-error">
          <img class="head" :src="defaultAvatar" alt="" />
        </div>
      </template>
    </el-image>
  </div>
</template>

<style scoped lang="scss">
.text-msg {
  display: flex;
  margin-bottom: 26px;
  .head {
    flex: 0 0 40px;
    width: 40px;
    height: 40px;
    margin-right: 8px;
    border-radius: 50%;
  }
  .msg-text {
    width: 100%;
    &.mine {
      text-align: right;
      padding-right: 10px;
      .text {
        background: rgba(1, 154, 129, 1);
        color: #fff;
        border-radius: 12px 0 12px 12px;
      }
      i {
        display: block;
        font-style: normal;
        text-align: right;
        font-size: 12px;
        color: #999;
      }
    }
    .name {
      color: rgba(25, 30, 40, 0.36);
      font-size: 12px;
      margin-bottom: 4px;
    }
    .text {
      display: inline-block;
      background: #fff;
      padding: 12px 20px;
      border-radius: 0 12px 12px 12px;
      max-width: 80%;
      box-shadow: 0 4px 12px -2px rgba(11, 13, 18, 0.06);
    }
  }
}
.order {
  display: flex;
  &.mine {
    justify-content: flex-end;
  }
  .order-item {
    background: #ffffff;
    width: 380px;
    border-radius: 12px 12px 12px 12px;
    overflow: hidden;
  }
  .header {
    height: 46px;
    background: rgba(29, 191, 115, 0.08);
    text-align: left;
    display: flex;
    align-items: center;
    padding: 0 16px;
    img {
      width: 16px;
      height: 16px;
      margin-right: 6px;
    }
    span {
      color: #1dbf73;
    }
  }
  .order-info {
    margin: 16px;
    background: rgba(25, 30, 40, 0.02);
    border-radius: 12px 12px 12px 12px;
    display: flex;
    align-items: center;
    padding: 10px;
    .img {
      width: 110px;
      height: 70px;
      margin-right: 12px;
      border-radius: 8px;
    }
    .info {
      display: flex;
      flex-direction: column;
      text-align: left;
      .name {
        font-size: 15px;
        color: #191e28;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        max-width: 206px;
        margin-bottom: 22px;
        font-weight: bold;
      }
      .price {
        text-align: right;
        color: #191e28;
        .days {
          font-weight: bold;
          padding-right: 12px;
          margin-right: 12px;
          border-right: 1px solid rgba(25, 30, 40, 0.08);
        }
        .size {
          font-weight: bold;
        }
      }
    }
  }
  .add-line {
    padding: 0 16px;
    p {
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      margin-bottom: 8px;
      &:last-child {
        margin-bottom: 0;
      }
      span:nth-child(1) {
        color: rgba(25, 30, 40, 0.6);
        padding-right: 10px;
      }
      span:nth-child(2) {
        flex: 1;
        color: rgba(25, 30, 40, 1);
        text-align: left;
      }
    }
  }
  .order-count {
    display: flex;
    align-items: center;
    padding: 12px 16px;
    .title {
      flex: 1;
      text-align: left;
      font-weight: bold;
    }
    .day {
      color: #191e28;
      font-weight: bold;
      margin-right: 16px;
    }
    .price {
      color: #ff4f4f;
      font-weight: bold;
      font-size: 16px;
      span {
        font-size: 12px;
      }
    }
  }
  .order-btn {
    padding: 8px 16px 16px;
    span {
      cursor: pointer;
      display: inline-block;
      width: 96px;
      height: 38px;
      text-align: center;
      line-height: 38px;
      font-size: 16px;
      color: #191e28;
    }
    .cancel {
      background: #ffffff;
      border-radius: 8px 8px 8px 8px;
      border: 1px solid rgba(25, 30, 40, 0.08);
    }
    .confirm {
      background: #1dbf73;
      border-radius: 8px 8px 8px 8px;
      margin-left: 12px;
      color: #fff;
    }
  }
}
</style>